package com.qfedu.stussm.controller;
import com.qfedu.stussm.utils.CodeImgUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class CodeController {
    //实现图片验证码
    @GetMapping("/api/img/code.do")
    public void create(HttpServletResponse response, HttpSession session) throws IOException {
        //1.生成随机验证码
        String code= CodeImgUtil.createCode(4);
        System.out.println(code);
        //2.存储到Session
        session.setAttribute("code",code);
        //3.生成验证码图片
        ImageIO.write(CodeImgUtil.createImg(code),"png",response.getOutputStream());
        response.flushBuffer();
    }
}
